package cn.edu.scau.cmi.wuzhenzhou.util;

import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class SessionFactoryUtil {
	private static SqlSessionFactory sqlSessionFactory;
	private static String resource = "mybatis-config.xml";
	
	private static SqlSessionFactory getSqlSessionFactory() throws Exception{
		if(sqlSessionFactory == null) {
			InputStream inputStream = Resources.getResourceAsStream(resource);
			sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		}
		return sqlSessionFactory;
	}
	
	public static SqlSession getSqlSession() throws Exception{
		SqlSession sqlSession = getSqlSessionFactory().openSession();
		return sqlSession;
	}
}
